<template>
	<scroll-view scroll-y="true" class="page">
		<u-navbar :placeholder="true" title="编辑活动" @leftClick="$utils.back()" border>

		</u-navbar>
		<view>
			<view style="align-items: center;" class="mt20 borderBottom  flex pb20 pt20">
				<view class="mr20 ">
					标题：
				</view>
				<view style="flex: 1;">
					<u--input placeholder="请输入标题" inputAlign='right' border="none" v-model="textarea"></u--input>
				</view>
			</view>
			<view class="mt20 borderBottom  flex pb20 pt20"
				style="align-items: center;justify-content: space-between;">
				<view class="mr20 ">
					封面：
				</view>
				<view>
					<view v-if="imageUrl.length==0" class=" upload flex" @click="openSelect('image')">
						<u-icon name="camera-fill" color="#D3D4D6" size="50"></u-icon>
					
					</view>
					<view v-if="imageShow">
						<imageUpload  type='radio' :imageShow='imageShow'
							@changeImageList='changeImageList' :imageUrl='imageUrl' @close='imageShow=null' />
					</view>
				</view>
			</view>

			<view style="align-items: center;" class="mt20 borderBottom  flex pb20 pt20">
				<view class="mr20 ">
					目标减重/Kg：
				</view>
				<view style="text-align: right;flex: 1;">
					<text class="iconfont icon-chizi" style="margin-right: 20rpx;" @click='showWeight=true' />
					<text @click="keyboardShow=true">
						{{weight}}Kg
					</text>
				</view>
			</view>
			<view style="align-items: center;" class="mt20 borderBottom  flex pb20 pt20">
				<view class="mr20 ">
					天数：
				</view>
				<view style="flex: 1;display: flex;justify-content: flex-end;">
					<u-number-box v-model="days" :min='1'>
						<view slot="minus" class="minus">
							<u-icon name="minus" size="12"></u-icon>
						</view>
						<view slot="plus" class="plus">
							<u-icon name="plus" color="#FFFFFF" size="12"></u-icon>
						</view>
					</u-number-box>
				</view>
			</view>
			<view style="align-items: center;" class="mt20 borderBottom  flex pb20 pt20">
				<view class="mr20 ">
					是否让别人参加：
				</view>
				<view style="flex: 1;display: flex;justify-content: flex-end;">
					<u-switch v-model="is_public"></u-switch>
				</view>
			</view>
			<view class="mt20 borderBottom  flex pb20 pt20"
				style="align-items: center;justify-content: space-between;" v-if='is_public'>
				<view class="mr20 ">
					群二维码：
				</view>
				<view>
					<view v-if="qr_group.length==0" class=" upload flex" @click="openSelect('qr_group')">
						<u-icon name="camera-fill" color="#D3D4D6" size="50"></u-icon>
					
					</view>
					<view v-if="groupImageShow">
						<imageUpload  type='radio' :imageShow='groupImageShow'
							@changeImageList='changeGroupImageList' :imageUrl='qr_group' @close='groupImageShow=null' />
					</view>
				</view>
			</view>
			<view style="align-items: center;" class="mt20 borderBottom  flex pb20 pt20">
				<view class="mr20 ">
					是否隐藏：
				</view>
				<view style="flex: 1;display: flex;justify-content: flex-end;">
					<u-switch v-model="is_hide"></u-switch>
				</view>
			</view>
			<view style="align-items: center;" class="mt20 borderBottom  flex pb20 pt20">
				<view class="mr20 ">
					开始时间：
				</view>
				<view style="flex: 1;display: flex;justify-content: flex-end;" @click="calendarShow=true">
					{{start_at?start_at:'选择开始时间'}}
				</view>
			</view>
			<u-popup :show="!(!showSelect)" :round="10" @close="showSelect=false">
				<view class="popupTitle">
					<u-icon name="close" color='#fff'></u-icon>
					<view>
						选择方式
					</view>
					<u-icon name="close" @click="showSelect=false"></u-icon>
				</view>
				<view style="height: 20vh;">
					<u-grid :border="false" @click="clickGrid">
						<u-grid-item v-for="(baseListItem,baseListIndex) in baseList" :key="baseListIndex">
							<u-icon :customStyle="{paddingTop:20+'rpx'}" :name="baseListItem.name" :size="22"></u-icon>
							<text class="grid-text">{{baseListItem.title}}</text>
						</u-grid-item>
					</u-grid>
				</view>
			</u-popup>
			<u-popup :show="showWeight" :round="10" @close="showWeight=false">
				<view class="popupTitle">
					<u-icon name="close" color='#fff'></u-icon>
					<view>
						体重
					</view>
					<u-icon name="close" @click="showWeight=false"></u-icon>
				</view>
				<view style="height: 24vh;" v-if="showWeight">
					<view style="text-align: center;font-size: 40rpx;color: green;margin: 20rpx 0;">
						{{weight}}Kg
					</view>
					<rulerComponent min='0' max='150' :int='false' :active='weight' @value="finishRuler" />
				</view>
			</u-popup>
			<u-keyboard :tooltip='false' ref="uKeyboard" mode="number" :show="keyboardShow" :safeAreaInsetBottom='true'
				@close='keyboardShow=false' @change='keyboardChange' @backspace='backspace' />
		</view>
		<view class="bottom viewWidth">
			<u-button :disabled="disabled" @click="pulish">保存</u-button>
		</view>
		<root-portal>
			<u-calendar :minDate='minDate()' :show="calendarShow" @close='calendarShow=false'
				@confirm='confirm'></u-calendar>
		</root-portal>
	</scroll-view>
</template>
<script>
	import rulerComponent from "@/components/sapling-vue-scale/sapling-vue-scale";
	import imageUpload from '@/components/imageUpload';
	export default {
		components: {
			imageUpload,
			rulerComponent
		},
		data() {
			return {
				groupImageShow: false,
				days: 30,
				keyboardShow: false,
				loading: true,
				showWeight: false,
				start_at: null,
				calendarShow: false,
				is_hide: false,
				is_public: true,
				conquer_id: null,
				weight: 1,
				baseList: [{
						name: 'photo',
						title: '本地图片'
					},
					{
						name: 'chat',
						title: '素材聊天'
					},
				],
				qr_group: [],
				imageUrl: [],
				textarea: null,
				showSelect: false,
				imageShow: null,
				disabled: false,
				loading: true,
			}
		},
		onLoad(op) {
			this.conquer_id = op.id
			this.$api.Conquer.Detail(this.conquer_id).then(res => {
				const detail = res.data.conquer
				this.weight = detail.weight
				this.textarea = detail.title
				this.is_public = detail.is_public ? true : false
				this.is_hide = detail.is_hide ? true : false
				this.start_at = detail.start_at
				this.days = detail.days
				this.imageShow = true
				this.imageUrl = [{
					url: detail.thumb,
					coverUrl: detail.thumb,
					id: null,
					upload: true,
					title: ''
				}]

				this.groupImageShow = true
				this.qr_group = [{
					url: detail.qr_group,
					coverUrl: detail.qr_group,
					id: null,
					upload: true,
					title: ''
				}]
				this.loading = false
			}).catch(err => {
				uni.$u.toast(err.data.message)
			})
		},

		methods: {
			backspace() {
				if (this.weight.toString().length == 1) {
					this.weight = 0
					return
				}
				this.weight = this.weight.toString().substring(0, this.weight.length - 1)


			},
			keyboardChange(e) {
				if (this.weight == 0) {
					this.weight = e
					return
				}
				if (this.weight.toString().length > 5) {
					return
				}
				if (this.weight.toString().includes('.') && e == ".") {
					return
				}
				this.weight += e.toString()
				if (this.weight > 150) {
					this.weight = "150"
					uni.$u.toast('体重不能超过150')
				}

			},
			finishRuler(e) {
				this.weight = e
			},
			confirm(e) {
				this.start_at = e[0]
				this.calendarShow = false
			},
			minDate() {
				// 创建一个表示当前时间的Date对象
				var today = new Date();

				// 使用getDate()方法获取当前日期，并加上1得到明天的日期
				var tomorrowDate = today.getDate() + 1;

				// 使用setDate()方法设置Date对象的日期为明天的日期
				today.setDate(tomorrowDate);

				// 获取明天的年份、月份和日期
				var tomorrowYear = today.getFullYear();
				var tomorrowMonth = today.getMonth() + 1; // 月份从0开始，所以要加1
				var tomorrowDay = today.getDate();

				return tomorrowYear + "-" + tomorrowMonth + "-" + tomorrowDay
			},
			clickGrid(e) {
				if (e == 0) {
					if (this.showSelect == 'image') {
						this.imageShow = 'photo'
					} else {
						this.groupImageShow = 'photo'
					}
					this.showSelect = false
				} else {
					if (this.showSelect == 'image') {
						this.imageShow = 'chat'
					} else {
						this.groupImageShow = 'chat'
					}
					this.showSelect = false
				}
			},

			openSelect(e) {
				this.showSelect = e
			},
			changeImageList(e) {
				console.log(e)
				this.imageUrl = e
			},

			pulish() {
				if (!this.imageUrl.every(item => item.upload) && this.imageUrl.length) {
					uni.$u.toast('正在上传中')
					return
				}

				if (!this.imageUrl.length) {
					uni.$u.toast('必须上传图片')
					return
				}

				if (!this.textarea) {
					uni.$u.toast('请输入内容')
					return
				}
				if (this.textarea.length < 6) {
					uni.$u.toast('内容长度不能小于6个字符')
					return
				}
				if (!this.start_at) {
					uni.$u.toast('必须选择开始时间')
					return
				}
				this.Add()

			},
			Add() {
				this.disabled = true
				let obj = {
					days: this.days,
					weight: this.weight,
					title: this.textarea,
					thumb: this.imageUrl[0].url,
					is_public: this.is_public ? 1 : 0,
					is_hide: this.is_hide ? 1 : 0,
					start_at: this.start_at,
					qr_group: this.qr_group[0]?.url
				}
				this.$api.Conquer.Update(this.conquer_id, obj).then(res => {
					uni.$u.toast('更新挑战成功')
					setTimeout(() => {
						uni.reLaunch({
							url: '/Home/index'
						})
					}, 500)
				}).catch(err => {
					this.disabled = false
					console.log(err)
					uni.$u.toast(err.data.message)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		display: flex;
		height: calc(100vh - constant(safe-area-inset-bottom) + 10rpx);
		height: calc(100vh - env(safe-area-inset-bottom) + 10rpx);
		flex-direction: column;
		padding: 0 20rpx;
		box-sizing: border-box;
	}

	.borderBottom {
		border-bottom: 1rpx solid #f1f1f1;
	}

	




	.flex {
		display: flex;
	}

	.pb20 {
		padding-bottom: 20rpx;
	}

	.pt20 {
		padding-top: 20rpx;
	}

	.upload {
		border-radius: 8rpx;
		background-color: #f5f5f5;
		width: 150rpx;
		height: 150rpx;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	::v-deep .u-border {
		border-color: #fff !important;
	}

	::v-deep .u-textarea {
		padding: 0 !important;
	}

	.selectItem {
		font-size: 30rpx;
		text-align: center;
		padding: 20rpx 0;
	}

	.selectItem:nth-child(1) {
		border-bottom: 1rpx solid #f1f1f1;
	}

	.userBox {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 20rpx 0;

		view:nth-child(2) {
			flex: 0.98;
		}
	}

	.bottom {
		width: calc(100% - 40rpx);
		margin-top: 20rpx;
	}

	.minus {
		width: 22px;
		height: 22px;
		border-width: 1px;
		border-color: #E6E6E6;
		border-style: solid;
		border-top-left-radius: 100px;
		border-top-right-radius: 100px;
		border-bottom-left-radius: 100px;
		border-bottom-right-radius: 100px;
		@include flex;
		justify-content: center;
		align-items: center;
	}

	.input {
		padding: 0 10px;
	}

	.plus {
		width: 22px;
		height: 22px;
		background-color: #FF0000;
		border-radius: 50%;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
	}
</style>